<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */
 
class history {

	var $id;
	var $fileid;
	var $comment;
	var $statusid;
	var $userid;
	var $date;
	
	var $prj;
	var $pre;
	var $subprj;
	var $teamid;
	
    public function __construct($prj)
    {
    	$this->prj=PROJECT_PREFIX.$prj;
    	$this->pre=$prj;
    }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		//$class = new ReflectionClass('files');	
		//return $class->newInstance();
		return new history($this->pre);
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".HISTORY.$this->prj." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function getLast() {
		$sql = "SELECT * FROM ".HISTORY.$this->prj." WHERE fileid = ".util::str($this->fileid)." order by id desc limit 1,1";
		return $sql;
	}
	
	public function listAll() {
		$sql = "SELECT * FROM ".HISTORY.$this->prj." WHERE fileid = ".util::str($this->fileid)." order by date desc";
		return $sql;
	}

	public function cleanTeam() {
		$sql = "DELETE h FROM ".HISTORY.$this->prj." h, ";
		$sql .= FILES.$this->prj." f WHERE f.id = h.fileid AND ";
		$sql .= " f.teamid = ".util::str($this->teamid);
		$sql .= " AND f.prjid = ".util::str($this->subprj);
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".HISTORY.$this->prj;
		$sql .= " WHERE fileid = ".util::str($this->fileid);
		return $sql;
	}
	
	public function insert() {
		$now = util::now();	
      	$sql = "INSERT INTO ".HISTORY.$this->prj." (fileid, statusid, userid, date, comment) ";
      	$sql .= "VALUES (".util::str($this->fileid).",".util::str($this->statusid).",";
      	$sql .= util::str($this->userid).",".util::str($now).",";
      	$sql .= util::str(util::encode($this->comment)).")";
		return $sql;
	}

	public function fill($array) {
		$this->id=$array['id'];
		$this->fileid=$array['fileid'];
		$this->userid=$array['userid'];
		$this->comment=util::decode($array['comment']);
		$this->statusid=$array['statusid'];
		$this->date=$array['date'];
	}

}

?>